﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DAO;
using DTO;
using PTExpress.Models;

namespace PTExpress.Controllers
{
    [Authorize]
    public class QLKhuyenMaiController : Controller
    {

        public ActionResult Index()
        {
            KhuyenMaiDAO khuyenmaiDAO = new KhuyenMaiDAO();
            List<KhuyenMaiDTO> ls = khuyenmaiDAO.GetAll();
            this.ViewData.Add("dsKhuyenmai", ls);
            return View();
        }

        public ActionResult Details(int id)
        {
            return View();
        }

        public ActionResult Them()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Them(KhuyenMai model)
        {
            ViewData["ModelState"] = ModelState.IsValid;

            bool thanhCong = false;
            if (ModelState.IsValid)
            {
                KhuyenMaiDAO khuyenmaiDAO = new KhuyenMaiDAO();
                
                KhuyenMaiDTO khuyenmaiDTO = new KhuyenMaiDTO();
                khuyenmaiDTO.Id = model.Id;
                khuyenmaiDTO.Phantram = model.Phantram;
                khuyenmaiDTO.Batdau = model.Batdau;
                khuyenmaiDTO.Ketthuc = model.Ketthuc;
                thanhCong = khuyenmaiDAO.AddNew(khuyenmaiDTO);
                if (thanhCong)
                {
                    return RedirectToAction("Index", "QLKhuyenMai");
                }
            }
            ViewData["ThanhCong"] = thanhCong;
            return View(model);
        }

        public ActionResult Sua(int id)
        {
            KhuyenMai model = new KhuyenMai();
            KhuyenMaiDAO khuyenmaiDAO = new KhuyenMaiDAO();
            KhuyenMaiDTO khuyenmaiDTO = khuyenmaiDAO.getById(id.ToString());
            ViewData["Id"] = khuyenmaiDTO.Id;
            model.Id = id;
            model.Phantram = khuyenmaiDTO.Phantram;
            model.Batdau = khuyenmaiDTO.Batdau;
            model.Ketthuc = khuyenmaiDTO.Ketthuc;
            return View(model);
        }


        [HttpPost]
        public ActionResult Sua(KhuyenMai model, String id)
        {
            KhuyenMaiDAO khuyenmaiDAO = new KhuyenMaiDAO();
            KhuyenMaiDTO khuyenmaiDTO = khuyenmaiDAO.getById(id);
            KhuyenMaiDTO newkhuyenmaiDTO = khuyenmaiDAO.getById(id);
            ViewData["Id"] = khuyenmaiDTO.Id;
            ViewData["ModelState"] = ModelState.IsValid;

            bool thanhCong = false;
            if (ModelState.IsValid)
            {
                newkhuyenmaiDTO.Batdau = model.Batdau;
                newkhuyenmaiDTO.Ketthuc = model.Ketthuc;
                newkhuyenmaiDTO.Phantram = model.Phantram;
                thanhCong = khuyenmaiDAO.Edit(khuyenmaiDTO,newkhuyenmaiDTO);
            }
            ViewData["ThanhCong"] = thanhCong;
            return View(model);
        }
 
    }
}
